package com.star.gc4.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class ResolveLrc {

	private static final String START = "00:00";

	public static Map<String,String> resolve(String LrcPath) throws IOException {
		BufferedReader reader = null;
		LinkedHashMap<String, String> lrcMap = new LinkedHashMap<>();
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(LrcPath)));
			String line;
			while ((line=reader.readLine())!=null){
				String[] arr = line.split("]", 2);
				String time = arr[0].substring(1,9);
				lrcMap.put(transfer(time),arr[1]);
			}
		}
		catch (Exception e){
			log.error("Message: {}",e.getMessage(),e);
		}
		finally {
			if (reader!=null){
				reader.close();
			}
		}
		return lrcMap;
	}

	public static String transfer(String end) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
		Date date = sdf.parse(end);
		Date date1 = sdf.parse(START);
		return "" + (date.getTime()-date1.getTime())/1000;
	}
}
